package com.xqkad.myfile.tools;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.util.Log;

public class OpenFile {
	
	private static Intent getHtmlFileIntent(File file) {
		Uri uri = Uri.parse(file.toString()).buildUpon()
				.encodedAuthority("com.android.htmlfileprovider")
				.scheme("content").encodedPath(file.toString()).build();
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.setDataAndType(uri, "text/html");
		return intent;
	}

	private static Intent getPdfFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/pdf");
		return intent;
	}

	private static Intent getTextFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "text/plain");
		return intent;
	}

	private static Intent getChmFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/x-chm");
		return intent;
	}

	private static Intent getWordFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/msword");
		return intent;
	}

	private static Intent getExcelFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/vnd.ms-excel");
		return intent;
	}

	private static Intent getPPTFileIntent(File file) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(file);
		intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
		return intent;
	}

	private static Intent getOtherFileIntent(File file) {
		/*
		 * Intent intent = new Intent();
		 * intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		 * intent.setAction(android.content.Intent.ACTION_DEFAULT);
		 */
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		// intent.setDataAndType(Uri.fromFile(file),
		// "application/vnd.android.package-archive");
		return intent;
	}

	private static Uri getUri(Context context, File file){
		Uri uri = null;
		final String authority = "com.xqkad.myfile.fileprovider";
		if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
			uri = FileProvider.getUriForFile(context, authority, file);
		}else {
			uri = Uri.fromFile(file);
		}
		return uri;
	}

	public static Intent getIntent(Context context, File file) {
		Intent intent = null;
		final String format = MyFormat.getFormat(file.getName());
		String type = "";
		//set type or return intent
		switch (format){
			case MyFormat.IMAGE:
				type = "image/*";
				break;
			case MyFormat.AUDIO:
				type = "audio/*";
				break;
			case MyFormat.VIDEO:
				type = "video/*";
				break;
			case MyFormat.APK:
				type = "application/vnd.android.package-archive";
				break;
			case MyFormat.TEXT:
				type = "text/plain";
				break;
			case MyFormat.WEBTEXT:
				return getHtmlFileIntent(file);

			case MyFormat.WORD:
				type = "application/msword";
				break;
			case MyFormat.EXCEL:
				type = "application/vnd.ms-excel";
				break;
			case MyFormat.PDF:
				type = "application/pdf";
				break;
			case MyFormat.PPT:
				type = "application/vnd.ms-powerpoint";
				break;
			case MyFormat.RAR:
				return null;
			case MyFormat.OTHER:
				return null;
		}

		if(type.equals(""))
			return null;
		intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
		intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		//intent.addCategory("android.intent.category.DEFAULT");
		intent.setDataAndType(getUri(context, file), type);

		return intent;
	}

}
